package com.etc.controller;

import com.etc.pojo.User;
import com.etc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

/**
 * @program: RookieSocialNetworking02
 * @description: 用户控制器
 * @author: 梁伟文
 * @create: 2020-08-14 20:23
 **/
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService service;
    @Autowired
    private JedisPool jedisPool;
    //登录
    @RequestMapping("/login")
    public ModelAndView login(User user, HttpSession session) {
        ModelAndView mv = new ModelAndView();
        User u = service.login(user);
        if (u != null) {
            session.setAttribute("user", u);
            mv.setViewName("redirect:/index.jsp");
            return mv;
        }
        mv.addObject("errorMsg", "用户名或密码错误,请重试~");
        mv.setViewName("forward:/login.jsp");
        return mv;
    }
    //验证码登录
    @RequestMapping("/logintwo")
    public ModelAndView logintwo(String userTel, HttpSession session) {
        ModelAndView mv = new ModelAndView();
        User u = service.queryUserByTelephone(userTel);
        System.out.println(u);
        if (u != null) {
            session.setAttribute("user", u);
            mv.setViewName("redirect:/index.jsp");
            return mv;
        }
        mv.addObject("errorMsg", "该手机还未注册~~");
        mv.setViewName("forward:/login.jsp");
        return mv;
    }
    //注册
    @RequestMapping("/register")
    @ResponseBody
    public boolean register(User user) {
        return service.register(user);
    }

    @RequestMapping(value = "/checkName", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String checkName(String userName) {
        User user = service.queryUserByName(userName);
        if (user != null) {
            return "用户名已存在";
        }
        return null;
    }

    @RequestMapping(value = "/checkCode", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String checkCode(String userTel,String code) {
        Jedis jedis = jedisPool.getResource();
        String c = jedis.get(userTel);
        if (code.equals(c)) {
            jedis.close();
            return "验证码正确";
        }
        jedis.close();
        return "验证码有误";
    }

    @RequestMapping(value = "/checkTel", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String checkTel(String userTel) {
        User user = service.queryUserByTelephone(userTel);
        System.out.println(user);
        if (user != null) {
            return "手机号已存在";
        }
        return null;
    }

    /**
     * 上传头像
     *
     * @param upload
     */
    @RequestMapping("/uploadUserImg")
    @ResponseBody
    public String uploadUserImg(MultipartFile upload, HttpSession session) {
        String path = "D://images";
        String filename = upload.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        filename = uuid + filename;
        User user = (User) session.getAttribute("user");
        user.setUserImg(filename);
        Boolean flag = service.updateUser(user);
        if (flag) {
            try {
                upload.transferTo(new File(path, filename));
                System.out.println("文件上传成功~");
                session.setAttribute("user",user);
                return filename;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("文件上传失败~");
        return null;
    }

        /**
         * 获取头像
         * @param fileName 头像地址
         */
        @RequestMapping("/getUserImg")
        @ResponseBody
        public byte[] getUserImg (String fileName) throws IOException {
            String path = "D://images";
            if(fileName.equals("null")){
                fileName = "labi.jpg";
            }
            File file = new File(path, fileName);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] bs = new byte[1024];
            int n;
            while ((n = fis.read(bs)) != -1) {
                bos.write(bs, 0, n);
            }
            fis.close();
            bos.close();
            return bos.toByteArray();
        }

        /**
         * 用户退出
         */
        @RequestMapping("/exit")
        public String exit (HttpSession session){
            session.removeAttribute("user");
            return "redirect:/login.jsp";
        }



    }
